package com.java.struts.base.util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;

public class HibernateUtil {

    private  static  SessionFactory sessionFactory = null;
    static {
        Configuration configure = new Configuration().configure();
        sessionFactory = configure.buildSessionFactory();
    }

    /**
     * 获取session
     * @return
     */
    public static Session getSession(){
        return  sessionFactory.openSession();
    }

    /**
     * 获取query
     * @param hql
     * @return
     */
    public static Query getQuery(String hql){
        Session session = getSession();
        Query query = session.createQuery(hql);
        return query;
    }

    /**
     * 关闭closeSessionFactory
     */
    public static void closeFactory(){
        sessionFactory.close();
    }
}
